package com.bida.core.mongo;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;

import com.mongodb.Mongo;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

/**
 * This is an abstract class for specific MongoDB "templates", each of which will define connection attribute to
 * a MongoDB database instance. The common attributes and getters/setters here are mostly used to accept configuration
 * variables from Spring. We define them in the abstract class so that each specific class can be simple and easy to understand.
 *
 */
public abstract class BaseMongoTemplate {
    private static final Logger logger = LoggerFactory.getLogger(BaseMongoTemplate.class);
    
    protected Integer port;
    protected String database;
    protected String host;
    protected String username;
    protected char[] password;

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public char[] getPassword() {
        return password;
    }

    public void setPassword(char[] password) {
        this.password = password;
    }

    protected MongoTemplate createTemplate(Mongo mongoClient) throws UnknownHostException {
        logger.info("==========connect to " + this.host + ", port=" + this.port + ",database=" + this.database
                + "============");
        return new MongoTemplate(mongoClient, this.database);
    }
   
    protected List<ServerAddress> getMongoReplicationSet() throws UnknownHostException {
        if (port == null) {
            this.port = 27017;
        }

        if (this.host.indexOf(",") < 0) {
            return Arrays.asList(new ServerAddress(this.host, this.port));
        } else {
            List<ServerAddress> rep = new ArrayList<ServerAddress>();
            String[] hosts = this.host.split(",");

            for (String mongoHost : hosts) {
                rep.add(new ServerAddress(mongoHost, port));
            }

            return rep;
        }
    }
    
    protected List<MongoCredential> getMongoCredentials() {
        List<MongoCredential> credentials = null;
        if (this.getUsername() != null && this.getPassword() != null) {
            credentials = Arrays.asList(MongoCredential.createMongoCRCredential(this.getUsername(),
                    this.getDatabase(), this.getPassword()));
        }
        return credentials;
    }
}
